<script>
import uniProxy from './util/uniPromisify'
import { wxLogin } from '@/api/user'
import { onLaunchPromise_resolve } from './util/helper'
import store from './store'
async function appLoginProcess() {
  try {
    const loginRes = await uniProxy.login({
      provider: 'weixin'
    })

    if (loginRes.errMsg !== 'login:ok') return

    const code = loginRes.code
    // fetch
    const res = await wxLogin(code)
    // 暂无用户信息
    if (res.code === 200) {
      //
      store.commit('UPDATE_WXLOGINIFNO', {
        token: res.data.token,
        //
        openId: res.data.openId
      })
    }

    onLaunchPromise_resolve()
  } catch (e) {
    console.log(e)
  }
}
export default {
  async onLaunch() {
    console.log('App Launch')
    // 清理缓存
    uni.clearStorageSync()

    try {
      await appLoginProcess()
    } catch (e) {
      console.log(e)
    }
  },
  onShow: function () {
    console.log('App Show')
  },
  onHide: function () {
    console.log('App Hide')
  }
}
</script>

<style lang="scss">
/*每个页面公共css */
// 生成全局样式
@import './common/scss/index.scss';
// 覆盖vant组件样式
page {
  --picker-action-text-color: #{$theme-color};
  --tabs-bottom-bar-color: #{$theme-color};
  --tabs-line-height: #{pxToRpx(50)};
}
</style>
